import { Axios, AxiosInstance } from 'axios';
import { CombatSkillTableConfig, DmgTableConfig, BuffTableConfig } from './TableVisualable'
// 定义外部数据接口 - 数据的请求和传输
export const TableDataApiConfig = {
  // baseUrl: 'http://localhost:8099/combat',
  // baseUrl: 'http://192.168.9.68:8099/combat',
  // port: 8099,
  basePath: '/combat',

    // 技能相关API
  [CombatSkillTableConfig.name]: {
    getAll: '/get_all_combat_cfg',
    getById: '/get_target_combat_cfg',
    update: '/update_combat_cfg',
    delCfg: '/del_cfg',
    mod: 3
  },

  // 伤害相关API
  [DmgTableConfig.name]: {
    getAll: '/get_all_dmg_cfg',
    getById: '/get_target_dmg_cfg',
    update: '/update_dmg_cfg',
    delCfg: '/del_cfg',
    mod: 4
  },

  // BUFF相关API
  [BuffTableConfig.name]: {
    getAll: '/get_all_buff_cfg',
    getById: '/get_target_buff_cfg',
    update: '/update_buff_cfg',
    delCfg: '/del_cfg',
    mod: 5
  },


}

// 查询配置数据
export function getAllDataCfg(
  http: Axios | undefined,
  ip: string,
  category: typeof CombatSkillTableConfig.name | typeof DmgTableConfig.name | typeof BuffTableConfig.name,
): Promise<CombatSkill[] | DmgData[] | BuffData[]> {

  if (!http) {
    return Promise.reject('http is undefined')
  }

  const categoryConfig = TableDataApiConfig[category] as {
    getAll: string;
    getById: string;
    update: string;
    delCfg: string;
    mod: number;
  };
  const url = `http://${ip}${TableDataApiConfig.basePath}/get_all_cfg?mod=${categoryConfig.mod}`
  return http?.get(url, {
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
  })
}

// 查询单个配置数据
export function getOneDataCfg(
  http: Axios | undefined,
  ip: string,
  category: typeof CombatSkillTableConfig.name | typeof DmgTableConfig.name | typeof BuffTableConfig.name,
  id: number
): Promise<CombatSkill | DmgData | BuffData> {

  if (!http) {
    return Promise.reject('http is undefined')
  }

  const categoryConfig = TableDataApiConfig[category] as {
    getAll: string;
    getById: string;
    update: string;
    delCfg: string;
    mod: number;
  };
  const url = `http://${ip}${TableDataApiConfig.basePath}/get_cfg?mod=${categoryConfig.mod}&id=${id}`
  return http?.get(url, {
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
  })
}

// 删除单个配置数据
export function delOneDataCfg(
  http: Axios | undefined,
  ip: string,
  category: typeof CombatSkillTableConfig.name | typeof DmgTableConfig.name | typeof BuffTableConfig.name,
  id: number,
): Promise<CombatSkill | DmgData | BuffData> {

  if (!http) {
    return Promise.reject('http is undefined')
  }

  const categoryConfig = TableDataApiConfig[category] as {
    getAll: string;
    getById: string;
    update: string;
    delCfg: string;
    mod: number;
  };
  const url = `http://${ip}${TableDataApiConfig.basePath}/del_cfg?mod=${categoryConfig.mod}&id=${id}`
  return http?.get(url, {
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
  })
}

// 更新单个配置数据
export function updateOneDataCfg(
  http: Axios | undefined,
  ip: string,
  category: typeof CombatSkillTableConfig.name | typeof DmgTableConfig.name | typeof BuffTableConfig.name,
  id: number,
  data: CombatSkillTableData | DmgDataTableData | BuffTableData
): Promise<CombatSkill | DmgData | BuffData> {

  if (!http) {
    return Promise.reject('http is undefined')
  }

  const categoryConfig = TableDataApiConfig[category] as {
    getAll: string;
    getById: string;
    update: string;
    delCfg: string;
    mod: number;
  };
  const url = `http://${ip}${TableDataApiConfig.basePath}/update_cfg?mod=${categoryConfig.mod}&id=${id}`
  return http?.post(url, data, {
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
  })
}




// 定义外部数据实体
export interface CombatSkill {
  id: number
  min_lev: number
  max_lev: number
  name: string
  type: number
  attack_type: number
  elem_type: number
  target_type: number
  active_type: number
  special_type: string
  speed: number
  pre_cooldown: number
  cooldown: number
  mp_template: number
  cost_point: string
  cost_anger: number
  self_cond: string
  self_loss: string
  buff_self: string
  buff_target: string
  before_scripts: string
  after_scripts: string
  dmg_id: number
  pass_check_script: string
  pass_script: string
  show_passive: number
  is_shout: number
  extra_data: string
}

// 伤害数据接口
export interface DmgData {
  dmg_id: number
  min_lev: number
  max_lev: number
  desc: string
  attack_times: number
  skill_eff_type: number
  self_mode: number
  target_mode: number
  target_list: string
  dmg_ratio: string
  dmg_addt: string
  dmg_scripts: string
}

// BUFF数据接口
interface BuffData {
  buff_id: number
  name: string
  type: number
  same_mode: number
  class: number
  lev: number
  max_class: string
  time_type: number
  eff_type: number
  eff_sec_type: number
  eff_thi_type: number
  init_layer: number
  max_layer: number
  can_dispel: number
  duration: number
  hit_ratio: number
  min_hit_ratio: number
  max_hit_ratio: number
  hit_script: string
  script_list: string
  revise_duration: number
  eff_notice: number
}

// 定义表格数据展示实体
export interface CombatSkillTableData {
  headerOp: number
  id: number
  min_lev: number
  max_lev: number
  name: string
  type: number
  attack_type: number
  elem_type: number
  target_type: number
  active_type: number
  special_type: string
  speed: number
  pre_cooldown: number
  cooldown: number
  mp_template: number
  cost_point: string
  cost_anger: number
  self_cond: string
  self_loss: string
  buff_self: string
  buff_target: string
  before_scripts: string
  after_scripts: string
  dmg_id: number
  pass_check_script: string
  pass_script: string
  show_passive: number
  is_shout: number
  extra_data: string
}

export interface DmgDataTableData {
  headerOp: number
  dmg_id: number
  min_lev: number
  max_lev: number
  desc: string
  attack_times: number
  skill_eff_type: number
  self_mode: number
  target_mode: number
  target_list: string
  dmg_ratio: string
  dmg_addt: string
  dmg_scripts: string
}

// todo
export interface BuffTableData {
  headerOp: number
  buff_id: number
  name: string
  type: number
  same_mode: number
  class: number
  lev: number
  max_class: string
  time_type: number
  eff_type: number
  eff_sec_type: number
  eff_thi_type: number
  init_layer: number
  max_layer: number
  can_dispel: number
  duration: number
  hit_ratio: number
  min_hit_ratio: number
  max_hit_ratio: number
  hit_script: string
  script_list: string
  revise_duration: number
  eff_notice: number
}

// 使用正确的函数重载方式
export function convertToTableData(data: DmgData): DmgDataTableData;
export function convertToTableData(data: CombatSkill): CombatSkillTableData;
export function convertToTableData(data: BuffData): BuffTableData;
export function convertToTableData(data: any): any {
  return {
    headerOp: 0,
    ...data
  }
}



